package guoanmaker.operator.business.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorEventDetails;
import guoanmaker.operator.business.model.OperatorPcActive;
import guoanmaker.operator.business.repository.OperatorEventDetailsRepository;
import guoanmaker.operator.business.repository.OperatorPcActiveRepository;

@Service
public class OperatorPcActiveService implements OperatorPcActiveServiceInterface {
	@Autowired
private OperatorPcActiveRepository operatorPcActiveRepository;
	@Autowired
	private OperatorEventDetailsRepository operatorEventDetailsRepository;
	 @PersistenceContext
		private EntityManager manager;
	@Override
	@Transactional
	public OperatorPcActive saveOperatorPcActive(OperatorPcActive operatorPcActive) {
		// TODO Auto-generated method stub
		return operatorPcActiveRepository.save(operatorPcActive);
	}
	@Override
	@Transactional
	public void deleteOperatorPcActive(String id) {
		// TODO Auto-generated method stub
		operatorPcActiveRepository.delete(id);
	}
	@Override
	public List<OperatorPcActive> selectOperatorPcActiveList(String cityId) {
		// TODO Auto-generated method stub
		List<OperatorPcActive> ocactives=operatorPcActiveRepository.getOperatorPcActive(cityId);
		if(ocactives.size()>0){
			for(OperatorPcActive ocs:ocactives){
				OperatorEventDetails operatorEventDetails=operatorEventDetailsRepository.getSelectEventByid(ocs.getActiveId());
				if(null==operatorEventDetails){
					ocs.setActiveId("");
				}
			}
		}
		return ocactives;
	}
	
	 @Transactional
	  public void delectAllBycitycode(String cityId){
		  String jpql="delete  from OperatorPcActive  u where u.cityId=:cityId";
		  Query query2=manager.createQuery(jpql);//查询商品提供的服务区域
			query2.setParameter("cityId", cityId);
			query2.executeUpdate();
		  
	  }

}
